package com.example.springboot.jpa.usermanager.controller;

import com.example.springboot.jpa.usermanager.entity.Role;
import com.example.springboot.jpa.usermanager.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

/**
 * @desc
 * @Author wangsh
 * @date 2018/4/14 10:50
 */
@RestController
@RequestMapping("/role")
public class RoleController {

	@Autowired
	private RoleService roleService;

	@RequestMapping("/save")
	@ResponseBody
	public Role save() {
		Role role = new Role();
		int id = new Random().nextInt(10000);
		role.setId(id);
		role.setName("管理员" + id);
		role.setDescription("管理员" + id);

		int result = this.roleService.insert(role);
		System.out.println(result);
		return role;
	}

	@RequestMapping("/getById")
	@ResponseBody
	public Role getById(Integer id) {
		Role role = this.roleService.getById(id);
		System.out.println(role);
		return role;
	}

	@RequestMapping("/update")
	public void update() {
		Role role = new Role();
		role.setId(1);
		role.setName("管理员11");
		this.roleService.update(role);
	}

	@RequestMapping("/deleteById")
	public void deleteById(Integer id) {
		int result = this.roleService.deleteById(id);
		System.out.println(result);
	}
}
